﻿@using System.Net.Http.Headers
@using System.Security.Cryptography
@using System.Net
@using Tool.Models
@using Tool.Services
@inject ISnackbar Snackbar
@inject IHttpClientFactory httpClientFactory
@inject LoginService loginService

<MudGrid>
    <MudItem xs="3">
        <MudSelect @bind-Value="_selectedSite" Label="选择环境" Variant="Variant.Text" Clearable Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.LocationOn">
            @foreach (var site in _sites)
            {
                <MudSelectItem Value="@site.SiteUrl">@site.Name</MudSelectItem>
            }
        </MudSelect>
    </MudItem>
    <MudItem xs="3">
        <MudTextField @bind-Value="userName" Label="用户名" Variant="Variant.Text" Adornment="Adornment.Start"
                      AdornmentIcon="@Icons.Material.Filled.LockPerson" Clearable="true" />
    </MudItem>
    <MudItem xs="3">
        <MudTextField @bind-Value="password" Label="密码" Variant="Variant.Text" InputType="@PasswordInput"
                      Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="ShowPassword" AdornmentAriaLabel="Show Password" Clearable="true" />
    </MudItem>
    <MudItem xs="3" Class="align-content-center">
        <MudButton Class="align-self-center" FullWidth="true" Disabled="@_processing" OnClick="OnLogin" Variant="Variant.Filled" Color="Color.Primary">
            @if (_processing)
            {
                <MudProgressCircular Class="ms-n1" Indeterminate="true" />
                <MudText Class="ms-2">Processing</MudText>
            }
            else
            {
                <MudText>登录</MudText>
            }
        </MudButton>
    </MudItem>

</MudGrid>

@code
{
    private string userName = "likeshan";
    private string password = "Sfqd@20220428";
    private bool _processing = false;
    private string _selectedSite = "zwy";
    private List<SiteInfo> _sites = new()
    {
        new SiteInfo { Name = "政务云", SiteUrl = "zwy" },
        new SiteInfo { Name = "上海", SiteUrl = "sh" },
        new SiteInfo { Name = "云南", SiteUrl = "yn" },
        new SiteInfo { Name = "首信", SiteUrl = "sx" }
    };
    bool isShow;
    InputType PasswordInput = InputType.Password;
    string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;

    private HttpResponseHeaders _responseHeaders;


    [Parameter]
    public EventCallback<LoginResponseDto> OnSuccess { get; set; }

    void ShowPassword()
    {
        @if (isShow)
        {
            isShow = false;
            PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
            PasswordInput = InputType.Password;
        }
        else
        {
            isShow = true;
            PasswordInputIcon = Icons.Material.Filled.Visibility;
            PasswordInput = InputType.Text;
        }
    }

    async Task OnLogin()
    {
        string loginUrl = $"/login/";
        string authenticateUrl = $"/authenticate/";
        string logoutUrl = "/logout/";
        _processing = true;
        try
        {
            _responseHeaders = await loginService.Login(_selectedSite);
            if (_responseHeaders == null)
            {
                Snackbar.Add("登录失败", Severity.Error);
            }
            else
            {
                await OnSuccess.InvokeAsync(new LoginResponseDto
                {
                    SelectedSite = _selectedSite,
                    ResponseHeaders = _responseHeaders
                });
                Snackbar.Add("登录成功", Severity.Success);
                Snackbar.Add("登录成功", Severity.Success);
            }
        }
        catch (HttpRequestException httpEx)
        {
            // 专门捕获 HTTP 层异常，可能包含内部异常
            Console.WriteLine($"HTTP 请求异常: {httpEx.Message}");
            if (httpEx.InnerException != null)
            {
                Console.WriteLine($"内部异常: {httpEx.InnerException.Message}");
                // 这里可能会看到 "The credentials supplied to the package were not recognized"
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
            if (ex is CryptographicException cryptoEx)
            {
                Console.WriteLine($"加密错误详情: {cryptoEx.Message}");
            }
        }
        finally
        {
            _processing = false;
        }
    }
}
